{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "083b4aa1",
   "metadata": {},
   "source": [
    "## numpy数组的排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb1d24df",
   "metadata": {},
   "source": [
    "np.sort函数，数组的排序:\n",
    " * 默认是使用数组的最后一个轴进行排序\n",
    " * 比python本身的排序函数执行效率高\n",
    " * np.sort()形式排序不会改变数组本身，ndarray.sort()会改变数组本身。\n",
    " * sort()函数为从小到大排序。\n",
    " * 如果想反向排序即从大到小，可以使用其他方法，如先给排序数据加负号，再把排序后的数据加负返回号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d932e2e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21e3107c",
   "metadata": {},
   "source": [
    "1. 普通的排序函数np.sort()和ndarry.sort() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "826785b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5, 10,  0,  1,  7],\n",
       "       [ 8,  1,  4,  0, 10],\n",
       "       [ 3,  0,  2,  4, 11]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=np.random.randint(0,12,size=(3,5))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cc23e356",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  5,  7, 10],\n",
       "       [ 0,  1,  4,  8, 10],\n",
       "       [ 0,  2,  3,  4, 11]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#默认针对最后一轴进行排序\n",
    "np.sort(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "855a833b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  0,  0,  0,  7],\n",
       "       [ 5,  1,  2,  1, 10],\n",
       "       [ 8, 10,  4,  4, 11]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以手动设置轴axis参数的值\n",
    "np.sort(d,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c528aec0",
   "metadata": {},
   "source": [
    "* 会改变数组本身的形式,ndarray.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c0e816ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 11, 13,  7,  3],\n",
       "       [ 6, 11, 11, 13, 13],\n",
       "       [ 6, 10,  1, 14,  9]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2=np.random.randint(0,15,size=(3,5))\n",
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8b0d6bd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "d2.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a0936e98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  7, 11, 13, 14],\n",
       "       [ 6, 11, 11, 13, 13],\n",
       "       [ 1,  6,  9, 10, 14]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01be765f",
   "metadata": {},
   "source": [
    "2. np.argsort(),返回排序后的下标值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fbd4d47d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 0, 4, 1],\n",
       "       [3, 1, 2, 0, 4],\n",
       "       [1, 2, 0, 3, 4]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5d1daf09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 0, 1, 0],\n",
       "       [0, 1, 2, 0, 1],\n",
       "       [1, 0, 1, 2, 2]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(d,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4915713",
   "metadata": {},
   "source": [
    "3. 排序方向的设置\n",
    "\n",
    "先让数组的负数排序，之后再将排序的数组前加负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cac321dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10,  7,  5,  1,  0],\n",
       "       [10,  8,  4,  1,  0],\n",
       "       [11,  4,  3,  2,  0]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-np.sort(-d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "aa0bbe26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10,  7,  5,  1,  0],\n",
       "       [ 7,  5,  0, 10,  1],\n",
       "       [ 7,  1,  5,  0, 10]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二种获取逆向(从大到小)的数据方法是见获取数组负数的下标，再使用np.take()函数获得\n",
    "indexs=np.argsort(-d)\n",
    "np.take(d,indexs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
